document.writeln(`
<style>
        #codeform{
            width: 500px;
            padding: 20px 50px;
            background-color: #eee;
            margin: auto;
            border-radius: 3%;
            line-height: 3em;
        }
        #codeform label{
            display: inline-block;
            width: 150px;
        }
        #codeform input{
            width: 300px;
            box-sizing: border-box;
            padding: 5px;
        }
    </style>
`)
Vue.component("generator", {
    template: `
    <form id="codeform" action="/code/generate" method="post" enctype="multipart/form-data">
        <h2>MyBatis-Plus 代码生成器</h2>
        <label>MySQL 脚本文件:</label><input id="_codefile" name="sql" type="file" @change="setPackage(true)" style="padding-left: 0px"><br>
        <label>包路径:</label><input v-model="p" name="packagePath" placeholder="请输入合法的包路径,参考:com.yc.demo"><br>
        <hr>
        <label></label><input type="submit" @click.prevent="check()" value="生成">
    </form>
    `,
    data(){return {p: ""}},
    methods: {
        setPackage(reset) {
            if (_codefile.files.length == 0) {
                alert("请选择 MySQL 导出的脚本文件!")
                return false;
            }
            let name = _codefile.files[0].name;
            if (!/([^\.]+)\.sql/i.test(name)) {
                alert("请选择 MySQL 导出的脚本文件!")
                _codefile.value = '';
                return false;
            }
            if(reset){
                suffix = "com.yc."
                name = name.replace(/([^\.]+)\.sql/i, "$1");
                name = name.replace(/\W/g, '');
                if (this.p) {
                    suffix = this.p.replace(/^(.+\.)\w+$/, "$1")
                }
                this.p = suffix + name;
            }
            return true;
        },
        check() {
            try {
                if (this.setPackage() == false) {
                    return;
                }
                if (!/^[a-zA-Z]\w*(\.[a-zA-Z]\w*){2,}$/.test(this.p)) {
                    alert('包路径不合法, 请重写填写!');
                    return false;
                }
                codeform.submit();
            } catch (e) {
                console.error(e);
            }
        }
    }
})